package cn.sharp.bbs.service.impl;

import cn.sharp.bbs.dao.gen.BillMapper;
import cn.sharp.bbs.dao.gen.RewardMapper;
import cn.sharp.bbs.dao.gen.RewardinteMapper;
import cn.sharp.bbs.dao.gen.UserMapper;
import cn.sharp.bbs.dao.score.ScoreDao;
import cn.sharp.bbs.entity.BO.ScoreEntity;
import cn.sharp.bbs.entity.DO.Bill;
import cn.sharp.bbs.entity.DO.Datadict;
import cn.sharp.bbs.entity.DO.Reward;
import cn.sharp.bbs.entity.DO.Rewardinte;
import cn.sharp.bbs.service.ScoreService;
import net.sf.ehcache.util.SetAsList;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * @className: ScoreServiceImpl
 * @description: 积分相关服务实现类
 * @author: 王晓崧
 * @date: 2022/7/10
 **/
@Transactional
@Service
public class ScoreServiceImpl implements ScoreService {
    @Resource
    private ScoreDao scoreDao;

    @Resource
    private RewardinteMapper rewardinteMapper;

    @Resource
    private RewardMapper rewardMapper;

    @Resource
    private BillMapper billMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 根据用户id和帖子id解锁积分内容，并在积分收支表中记录
     *
     * @param u_id   用户id
     * @param postID 帖子id
     */
    @Override
    public void unlockContent(Long u_id, Long postID) {
        Rewardinte rewardinte = new Rewardinte();//新建解锁积分内容类
        rewardinte.setUserId(u_id);//设置用户id
        rewardinte.setPostId(postID);//设置帖子id
        rewardinte.setNum(scoreDao.getPostScore(postID));//设置积分值
        rewardinte.setTime(new Date());//设置解锁时间

        rewardinteMapper.insert(rewardinte);//插入表

        Bill bill = new Bill();//新建账单
        bill.setUserSend(u_id);//设置积分发送者id
        bill.setUserGet(scoreDao.getPostPublisher(postID));//设置积分接收者id
        bill.setNum(rewardinte.getNum());//设置积分变动数额
        bill.setTime(new Date());//设置积分变动时间
        bill.setFrom(postID);//设置积分变动场所
        //设置类型
        Datadict datadict = new Datadict();
        datadict.setType("bill_type");
        datadict.setName("积分内容");
        bill.setType(scoreDao.getCodeByDict(datadict));

        billMapper.insert(bill);//插入账单
    }

    /**
     * 根据用户id和帖子id完成相应帖子悬赏，并在积分收支表中记录
     *
     * @param scoreEntity 包括了悬赏完成者id和帖子id
     * @param u_id        用户id
     */
    @Override
    public void completeReward(ScoreEntity scoreEntity, Long u_id) {
        Rewardinte rewardinte = new Rewardinte();//新建解锁积分内容类
        rewardinte.setUserId(scoreEntity.getUserID());//设置悬赏完成者id
        rewardinte.setPostId(scoreEntity.getPostID());//设置帖子id
        rewardinte.setNum(scoreDao.getPostScore(scoreEntity.getPostID()));//设置积分值
        rewardinte.setTime(new Date());//设置悬赏完成时间
        rewardinteMapper.insert(rewardinte);//插入表

        Bill bill = new Bill();//新建账单
        bill.setUserSend(u_id);//设置积分发送者id
        bill.setUserGet(scoreEntity.getUserID());//设置积分接收者id
        bill.setNum(rewardinte.getNum());//设置积分变动数额
        bill.setTime(new Date());//设置积分变动时间
        bill.setFrom(scoreEntity.getPostID());//设置积分变动场所
        //设置类型
        Datadict datadict = new Datadict();
        datadict.setType("bill_type");
        datadict.setName("悬赏");
        bill.setType(scoreDao.getCodeByDict(datadict));

        billMapper.insert(bill);//插入账单
    }

    /**
     * 在打赏表中记录打赏帖子id，打赏者id，打赏数额，打赏备注，并在积分收支表中记录
     *
     * @param u_id        打赏者id
     * @param scoreEntity 包括打赏帖子id，打赏数额，打赏备注
     */
    @Override
    public void rewardPost(Long u_id, ScoreEntity scoreEntity) {
        Reward reward = new Reward();//新建打赏类
        reward.setPostId(scoreEntity.getPostID());//设置打赏帖子
        reward.setNum(scoreEntity.getNum());//设置打赏数额
        reward.setTime(new Date());//设置打赏时间
        reward.setUserId(u_id);//设置打赏者id
        reward.setMark(scoreEntity.getMark());//设置打赏备注
        rewardMapper.insert(reward);//插入打赏表

        Bill bill = new Bill();//新建账单
        bill.setUserSend(u_id);//设置积分发送者id
        bill.setUserGet(scoreDao.getPostPublisher(scoreEntity.getPostID()));//设置积分接收者id
        bill.setNum(reward.getNum());//设置积分变动数额
        bill.setTime(new Date());//设置积分变动时间
        bill.setFrom(scoreEntity.getPostID());//设置积分变动场所
        //设置类型
        Datadict datadict = new Datadict();
        datadict.setType("bill_type");
        datadict.setName("打赏");
        bill.setType(scoreDao.getCodeByDict(datadict));

        billMapper.insert(bill);//插入账单
    }

    /**
     * 检查请求发起者是否是发帖者本人
     *
     * @param u_id   用户id
     * @param postID 帖子id
     * @return true 代表是 false 代表不是
     */
    @Override
    public boolean checkOwner(Long u_id, Long postID) {

        return scoreDao.checkOwner(u_id, postID) != 0;
    }

    /**
     * 检查请求发起者是否已经解锁过该贴积分内容
     *
     * @param u_id   用户id
     * @param postID 帖子id
     * @return true 代表已解锁 false 代表未解锁
     */
    @Override
    public boolean checkUnlock(Long u_id, Long postID) {

        return scoreDao.checkUnlock(u_id, postID) != 0;
    }

    /**
     * 检查悬赏是否已经被完成
     *
     * @param postID 帖子id
     * @return true 代表已完成 false 代表未完成
     */
    @Override
    public boolean checkCompleted(Long postID) {
        return scoreDao.checkCompleted(postID) != 0;
    }

    /**
     * 检查用户积分是否足够
     *
     * @param u_id   用户id
     * @param postID 帖子id
     * @return true 代表足够 false 代表不够
     */
    @Override
    public boolean checkIsEnough(Long u_id, Long postID) {
        Integer userScore = scoreDao.getUserScore(u_id);
        Integer postScore = scoreDao.getPostScore(postID);
        if (userScore == null) {
            System.out.println(userScore);
            return false;
        }
        if (postScore == null) {
            return true;
        }
        return userScore.compareTo(postScore) >= 0;
    }

    /**
     * 检查用户积分是否足够
     *
     * @param u_id      用户id
     * @param rewardNum 积分数额
     * @return true 代表足够 false 代表不够
     */
    @Override
    public boolean checkIsEnough(Long u_id, Integer rewardNum) {
        Integer userScore = scoreDao.getUserScore(u_id);
        return userScore.compareTo(rewardNum) >= 0;
    }

    /**
     * 获得积分内容
     *
     * @param postID 帖子id
     * @param u_id   用户id
     */
    @Override
    public ScoreEntity getIntegralContent(Long postID, Long u_id) {
        ScoreEntity scoreEntity = new ScoreEntity();//新建一个前后端交互类
        if (checkUnlock(u_id, postID) || checkOwner(u_id, postID)) {//如果已经解锁,或者是帖子发布者
            scoreEntity.setCheck(true);//设置check为true
            scoreEntity.setMark(scoreDao.getIntegralContent(postID));//设置积分内容
        } else {//未解锁
            scoreEntity.setCheck(false);//设置check为false
            scoreEntity.setNum(scoreDao.getPostScore(postID));//设置所需积分数额
        }
        return scoreEntity;
    }

    /**
     * 获得帖子打赏列表
     *
     * @param postID 帖子id
     */
    @Override
    public List<ScoreEntity> getRewardList(Long postID) {
        List<Reward> rewardList = scoreDao.getRewardList(postID);//获得帖子打赏列表
        List<ScoreEntity> scoreEntityList = new ArrayList<>();
        for (Reward reward : rewardList) {
            ScoreEntity scoreEntity = new ScoreEntity();

            scoreEntity.setUserName(userMapper.selectByPrimaryKey(reward.getUserId()).getName());
            scoreEntity.setNum(reward.getNum());
            scoreEntity.setMark(reward.getMark());
            scoreEntity.setTime(reward.getTime());

            scoreEntityList.add(scoreEntity);
        }
        return scoreEntityList;
    }

    @Override
    public Boolean checkHasCont(Long postID) {
        return scoreDao.checkHasCont(postID) > 0;
    }
}
